package com.my.service;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.my.domain.MyPressureData;
import com.my.mapper.MyPressureDataMapper;
import com.my.vo.PressureVo;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class MyPressureDataService extends ServiceImpl<MyPressureDataMapper, MyPressureData> {

    public List<List<PressureVo>> pressureList(String id,String name) {
        List<List<PressureVo>> pressureList = new ArrayList<>();
        List<PressureVo> myPressureDataList = baseMapper.pressureList(new QueryWrapper<>(MyPressureData.class)
                .eq(id != null, "mpd.paper_id", id)
                .eq(name != null, "mp.name", name));
        if (CollUtil.isEmpty(myPressureDataList)) {
            return pressureList;
        }
        Map<Integer, List<PressureVo>> groupedByTestNo = myPressureDataList.stream()
                .collect(Collectors.groupingBy(
                        PressureVo::getTestNo,       // 分组依据
                        Collectors.toList()              // 收集为List
                ));

        groupedByTestNo.forEach((k, v) ->
                pressureList.add(v));
        return pressureList;
    }

    public void paperPressureDeleted(String id) {
        removeById(id);
    }

    public void pressureEdit(MyPressureData myPressureData) {
        saveOrUpdate(myPressureData);
    }
}
